import logging
import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"

import os
import sys
import time
from concurrent.futures import ThreadPoolExecutor

sys.path.append('/opt/admin')

from api import settings
from rag.settings import print_rag_settings

from werkzeug.serving import run_simple
from api.apps import app
from api.db.db_models import init_database_tables as init_web_db
from api.db.services.document_service import DocumentService

PORT = 8089


def update_progress():
    while True:
        time.sleep(3)
        try:
            DocumentService.update_progress()
        except Exception:
            logging.exception("update_progress exception")


if __name__ == '__main__':
    print(r"""
            ____   ___    ______ ______ __               
           / __ \ /   |  / ____// ____// /____  _      __
          / /_/ // /| | / / __ / /_   / // __ \| | /| / /
         / _, _// ___ |/ /_/ // __/  / // /_/ /| |/ |/ / 
        /_/ |_|/_/  |_|\____//_/    /_/ \____/ |__/|__/                             

        """)
    settings.init_settings()
    print_rag_settings()
    # 初始化数据库
    init_web_db()

    thread = ThreadPoolExecutor(max_workers=1)
    thread.submit(update_progress)

    run_simple(
        hostname="0.0.0.0",
        port=PORT,
        application=app,
        threaded=True,
        use_reloader=False,
        use_debugger=False,
    )
